{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python385jvsc74a57bd0ae7890921ac3c17143ff000ac7152addc6614e2051824da39aa37dab63d26d82",
   "display_name": "Python 3.8.5 64-bit ('base': conda)"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "{'zs': 66, 'ls': 100, 'ww': 88}"
      ]
     },
     "metadata": {},
     "execution_count": 9
    }
   ],
   "source": [
    "# 掌握5大金刚之string，和9字口诀：基建-增删改查操-切复\n",
    "#5大金刚包括：list, tuple, str, set集合, dict\n",
    "\n",
    "#1. 基础 - 键值对，key:value,key 必须是immutable的对象\n",
    "#创建\n",
    "s = {'zs':66,'ls':100,'ww':88}#set也用大括号，直接{}是创建一个空的字典\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "{1: 99, 2: 88, 3: 100}"
      ]
     },
     "metadata": {},
     "execution_count": 10
    }
   ],
   "source": [
    "s = dict()#创建一个空的字典\n",
    "s = dict([(1,99),(2,88),(3,100)])#成对的数据的iterable\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "{'zs': 78, 'lis': 23, 'ww': 69, 'wb': 87}"
      ]
     },
     "metadata": {},
     "execution_count": 11
    }
   ],
   "source": [
    "names = ['zs','lis','ww','wb']\n",
    "scores = [78,23,69,87]\n",
    "s = dict(zip(names,scores))\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "{'zs': 78, 'lis': 23, 'ww': 69, 'wb': 87, 'ruhua': 89, 'damei': 76}"
      ]
     },
     "metadata": {},
     "execution_count": 12
    }
   ],
   "source": [
    "#增\n",
    "s['ruhua'] = 89\n",
    "\n",
    "s['damei'] = 76\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "{'zs': 78, 'lis': 23, 'ww': 69, 'wb': 87, 'damei': 76}"
      ]
     },
     "metadata": {},
     "execution_count": 13
    }
   ],
   "source": [
    "#删 del pop,popitem,clear\n",
    "del s['ruhua']\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "76"
      ]
     },
     "metadata": {},
     "execution_count": 14
    }
   ],
   "source": [
    "s.pop('damei')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "{'zs': 78, 'lis': 23, 'ww': 69}"
      ]
     },
     "metadata": {},
     "execution_count": 15
    }
   ],
   "source": [
    "s.popitem()#随机删除\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "s.clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "metadata": {},
     "execution_count": 17
    }
   ],
   "source": [
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "{'zs': 78, 'lis': 23, 'ww': 69, 'wb': 87, 'ruhua': 89, 'damei': 76}"
      ]
     },
     "metadata": {},
     "execution_count": 19
    }
   ],
   "source": [
    "names = ['zs','lis','ww','wb','ruhua','damei']\n",
    "scores = [78,23,69,87,89,76]\n",
    "s = dict(zip(names,scores))\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "{'zs': 78, 'lis': 23, 'ww': 69, 'wb': 87, 'ruhua': 89, 'damei': 99}"
      ]
     },
     "metadata": {},
     "execution_count": 20
    }
   ],
   "source": [
    "#改\n",
    "s['damei'] = 99\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "metadata": {},
     "execution_count": 22
    }
   ],
   "source": [
    "#查 get, in, not in\n",
    "'ww' in s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "69"
      ]
     },
     "metadata": {},
     "execution_count": 23
    }
   ],
   "source": [
    "s['ww']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "69"
      ]
     },
     "metadata": {},
     "execution_count": 24
    }
   ],
   "source": [
    "s.get('ww')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "'google.com'"
      ]
     },
     "metadata": {},
     "execution_count": 25
    }
   ],
   "source": [
    "s.setdefault('www','google.com')\n",
    "s['www']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "zs 78\nlis 23\nww 69\nwb 87\nruhua 89\ndamei 99\nwww google.com\n"
     ]
    }
   ],
   "source": [
    "#操作 遍历，items,keys,values,sort\n",
    "for k in s:\n",
    "    print(k,s[k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "['zs', 'lis', 'ww', 'wb', 'ruhua', 'damei', 'www']\n[78, 23, 69, 87, 89, 99, 'google.com']\n[('zs', 78), ('lis', 23), ('ww', 69), ('wb', 87), ('ruhua', 89), ('damei', 99), ('www', 'google.com')]\n"
     ]
    }
   ],
   "source": [
    "print(list(s.keys()))\n",
    "print(list(s.values()))\n",
    "print(list(s.items()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "{'damei': 99,\n",
       " 'lis': 23,\n",
       " 'ruhua': 89,\n",
       " 'wb': 87,\n",
       " 'ww': 69,\n",
       " 'www': 'google.com',\n",
       " 'zs': 78}"
      ]
     },
     "metadata": {},
     "execution_count": 29
    }
   ],
   "source": [
    "s2 = {k:v for k,v in sorted(s.items())}\n",
    "s2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "{'zs': 78,\n",
       " 'lis': 23,\n",
       " 'ww': 69,\n",
       " 'wb': 87,\n",
       " 'ruhua': 89,\n",
       " 'damei': 99,\n",
       " 'www': 'google.com'}"
      ]
     },
     "metadata": {},
     "execution_count": 30
    }
   ],
   "source": [
    "#切片 fromkeys\n",
    "#复制\n",
    "s4 = s.copy()\n",
    "s4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "{'zs': 78, 'lis': 23, 'ww': 69, 'wb': 87, 'ruhua': 89, 'damei': 99, 'www': 'google.com'}\n{'ss': 111, 'ww': 23}\n"
     ]
    }
   ],
   "source": [
    "s5 = {'ss':111,'ww':23}\n",
    "print(s)\n",
    "print(s5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "{'zs': 78,\n",
       " 'lis': 23,\n",
       " 'ww': 23,\n",
       " 'wb': 87,\n",
       " 'ruhua': 89,\n",
       " 'damei': 99,\n",
       " 'www': 'google.com',\n",
       " 'ss': 111}"
      ]
     },
     "metadata": {},
     "execution_count": 32
    }
   ],
   "source": [
    "s.update(s5)\n",
    "s"
   ]
  }
 ]
}